import { create, StateCreator } from 'zustand';
import { devtools } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import type { State,CustomRecord } from './types';
import { useEffect } from "react";
import { http } from "@/utils/request";
export type Actions = {
  getDataList(): void; // 执行查询
  emergencyList(): void; // 应急联系人执行查询
  // onPageChange(pagination: Page): void; // 分页、排序、筛选变化时触发
  // setQueryForm(form: Form): Promise<void>; // 查询表单
  openAddOrEditChildrenModal(id?:string): void; // 打开新增子女信息弹窗
  editChildrenData(id?:string): void; // 打开编辑子女信息弹窗
  addDataToChildrenTable(obj:CustomRecord): void;//添加子女信息到表格
  setDataToChildrenTable(obj:CustomRecord): void;//修改子女信息到表格
  delChildrenData(obj:CustomRecord): void;//删除子女信息表格数据

  openAddOrEditEmergencyModal(id?:string): void; // 打开新增应急联系人信息弹窗
  editEmergencyData(id?:string): void; // 打开编辑应急联系人信息弹窗
  addDataToEmergencyTable(obj:CustomRecord): void;//添加应急联系人信息到表格
  setDataToEmergencyTable(obj:CustomRecord): void;//修改应急联系人信息到表格
  delEmergencyData(obj:CustomRecord): void;//删除应急联系人信息表格数据
};
export type Store = State<CustomRecord> & Actions;
export const createStore: StateCreator<Store, [['zustand/immer', never]]> = (set, get) => ({
  //子女信息表格数据
  childrenTable: {
    index:0,
    data: [],
  },
  //子女信息表格数据
  getDataList: async() => {
    try {
      const res = await http.get({
        url:'/PmPersonChildren/qryList',
        params:{
          personId:'81800628'
        }
      })
      if(res.code == 200){
        set((state) => {
          state.childrenTable.data = res.rows
        });
      }else{
        
      }
    } catch (errorInfo) {
      console.log(errorInfo);
    }
    
  },
  //应急联系人表格数据
  emergencyTable:{
    index:0,
    data: []
  },
  //应急联系人表格数据
  emergencyList: async() => {
    try {
      const res = await http.get({
        url:'/PmPersonContact/qryList',
        params:{
          personId:'81800628'
        }
      })
      if(res.code == 200){
        set((state) => {
          state.emergencyTable.data = res.rows
        });
      }else{
        
      }
    } catch (errorInfo) {
      console.log(errorInfo);
    }
    
  },
  //子女详细信息
  childrenDetailData:{
    name:''
  },
  //应急联系人详细信息
  emergencyDetailData:{
    name1:"",
    name2:"",
    name3:"",
  },
  // 新增/编辑子女信息
  addOrEditChildrenModal: {
    open: false, // 弹窗开关
  },
  // 新增/编辑应急联系人
  addOrEditEmergencyModal: {
    open: false, // 弹窗开关
  },
  //保存结果弹窗
  resultModal: {
    open: false, // 弹窗开关
  },
  // 打开新增子女信息弹窗
  openAddOrEditChildrenModal:async (id:string) => {
    set((state) => {
      state.addOrEditChildrenModal.open=true
      state.childrenDetailData={}
    });
  },
  // 打开编辑子女信息弹窗
  editChildrenData:(value:CustomRecord,index:Number)=>{
    set((state) => {
      state.childrenTable.index = index
      state.childrenDetailData=value
      state.addOrEditChildrenModal.open=true
    });
  },
  // 新增子女信息
  addDataToChildrenTable:(obj:CustomRecord) =>{
    set((state) => {
      state.childrenTable.data.push({...obj});
    });
    get().getDataList()
  },
  // 修改子女信息
  setDataToChildrenTable:(obj:CustomRecord) =>{
    set((state) => {
      state.childrenTable.data[state.childrenTable.index] = {...obj};
    });
    get().getDataList()
  },
  // 删除子女信息
  delChildrenData:async (value:CustomRecord,index:Number) =>{
    try {
      const res = await http.delete({
        url:'/PmPersonChildren/remove',
        params:{
          deleteId: value.id
        }
      })
      if(res.code == 200){
        console.log(res)
        get().getDataList()
      }else{
        
      }
    } catch (errorInfo) {
      console.log(errorInfo);
    }
  },


  // 打开新增应急联系人信息弹窗
  openAddOrEditEmergencyModal:(id:string) => {
    set((state) => {
      state.addOrEditEmergencyModal.open=true
      state.emergencyDetailData={}
    });
  },
  // 打开编辑应急联系人信息弹窗
  editEmergencyData:(value)=>{
    set((state) => {
      state.emergencyDetailData=value
      state.addOrEditEmergencyModal.open=true
    });
  },
  // 新增应急联系人
  addDataToEmergencyTable:(obj:CustomRecord) =>{
    set((state) => {
      state.emergencyTable.data.push({...obj});
    });
    get().emergencyList()
  },
  // 编辑应急联系人
  setDataToEmergencyTable:(obj:CustomRecord) =>{
    set((state) => {
      state.emergencyTable.data[state.emergencyTable.index] = {...obj};
    });
    get().emergencyList()
  },
  // 删除应急联系人
  delEmergencyData:async (value:CustomRecord,index:Number) =>{
    try {
      const res = await http.delete({
        url:'/PmPersonContact/remove',
        params:{
          deleteId: value.id
        }
      })
      if(res.code == 200){
        get().emergencyList()
      }else{
        
      }
    } catch (errorInfo) {
      console.log(errorInfo);
    }
  },
});

export const useStore = create<Store>()(devtools(immer<Store>(createStore)));